package com.softer.chengxin.file.dao.provider;

import com.softer.chengxin.file.entity.FileDTO;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * @author Gong Zhiyang
 * @date 2018/10/16
 */
public class FileDaoProvider {
    /**
     * 通过文件类型查询文件
     * (
     *
     * @param map 查询实体
     * @return 文件信息
     */
    public String getFileList(HashMap<String, Object> map) {
        StringBuffer sql = new StringBuffer(20);
        FileDTO fileDTO = (FileDTO) map.get("f");
        Integer id = (Integer) map.get("id");
        sql.append("SELECT id,file_name as fileName,file_name as name,new_file_name as newFilName,DATE_FORMAT(add_time, '%Y-%m-%d') as addTime,type,");
        sql.append("file_size as fileSize,corid as corId,project_id as projectId,person_id as personId,path,path as url,");
        sql.append("info_id as infoId,file_type as fileType FROM `file_list` WHERE type=#{f.type}");
        if (id!=null){
            sql.append(" and corid=#{id}");
        }
        if (StringUtils.isNotBlank(fileDTO.getProjectId())) {
            sql.append(" and project_id=#{f.projectId}");
        }
        if (StringUtils.isNotBlank(fileDTO.getPersonId())) {
            sql.append(" and person_id=#{f.personId}");
        }
        if (StringUtils.isNotBlank(fileDTO.getInfoId())) {
            sql.append("  and info_id=#{f.infoId}");
        }
        return sql.toString();
    }
    /**
     * 通过文件类型查询文件
     * (
     *
     * @param map 查询实体
     * @return 文件信息
     */
    public String getPath(HashMap<String, Object> map) {
        StringBuffer sql = new StringBuffer(20);
        FileDTO fileDTO = (FileDTO) map.get("f");
        Integer id = (Integer) map.get("id");
        sql.append("SELECT path FROM `file_list` WHERE type=#{f.type}");
        if (id!=null){
            sql.append(" and corid=#{id}");
        }
        if (StringUtils.isNotBlank(fileDTO.getProjectId())) {
            sql.append(" and project_id=#{f.projectId}");
        }
        if (StringUtils.isNotBlank(fileDTO.getPersonId())) {
            sql.append(" and person_id=#{f.personId}");
        }
        if (StringUtils.isNotBlank(fileDTO.getInfoId())) {
            sql.append("  and info_id=#{f.infoId}");
        }
        return sql.toString();
    }
}
